﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmCapnhattrinhdochinhtri : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.CochinhtriDAL _dal = new DAL.EntitiesDAL.CochinhtriDAL();
        BLL.EntitiesBLL.CochinhtriBLL _bll = new BLL.EntitiesBLL.CochinhtriBLL();
        DAL.EntitiesDAL.NhanvienDAL _dal1 = new DAL.EntitiesDAL.NhanvienDAL();

        DataTable _dtb = new DataTable();
        DataTable _dtb1 = new DataTable();
        DataTable _dtb2 = new DataTable();
        DataTable _dtb3 = new DataTable();
        #endregion

        #region Inits
        public FrmCapnhattrinhdochinhtri()
        {
            InitializeComponent();
        }
        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);

            ClearDataBindings();
            ChangeStatus(false);
            cbo_Manhanvien.ResetText();
            cbo_Machinhtri.ResetText();
            cbo_Chungnhan.ResetText();
            _dtb1 = _dal1.Select_Nhanvien();
            _dtb2 = _dal.Select_Chinhtri();
            _dtb3 = _dal.Select_Chungnhan();
            cbo_Manhanvien.Properties.DataSource = _dtb1;
            cbo_Machinhtri.Properties.DataSource = _dtb2;
            cbo_Chungnhan.Properties.DataSource = _dtb3;
            txt_Noicap.ResetText();

            base.PerformAdd();

        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvCochinhtri.GetFocusedRowCellValue("MA_TDCT") + "";
            var id2 = grvCochinhtri.GetFocusedRowCellValue("MANHANVIEN") + "";
            DialogResult result;
            var ok = _dal.Delete_Chinhtri(id1, id2);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            cbo_Manhanvien.Properties.ReadOnly = true;

            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();
            bto_Search.Visible = false;

            if (_dtb != null)
            {

                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                var name_Noicap = Proper(txt_Noicap.Text);
                var o = new DAL.Entities.Cochinhtri()
                {
                    MA_TDCT = Convert.ToString(cbo_Machinhtri.EditValue),
                    MANHANVIEN = Convert.ToString(cbo_Manhanvien.EditValue),
                    CHUNGNHAN=Convert.ToString(cbo_Chungnhan.EditValue),
                    NGAYCAP_CNCT=Convert.ToDateTime(dte_Ngaycap.EditValue),
                    NOICAP_CNCT = name_Noicap
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var name_Noicap = Proper(txt_Noicap.Text);
                var o = new DAL.Entities.Cochinhtri()
                {
                    MA_TDCT = Convert.ToString(cbo_Machinhtri.EditValue),
                    MANHANVIEN = Convert.ToString(cbo_Manhanvien.EditValue),
                    CHUNGNHAN = Convert.ToString(cbo_Chungnhan.EditValue),
                    NGAYCAP_CNCT = Convert.ToDateTime(dte_Ngaycap.EditValue),
                    NOICAP_CNCT = name_Noicap
                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            cbo_Manhanvien.ResetText();
            cbo_Machinhtri.ResetText();
            cbo_Chungnhan.ResetText();
            dte_Ngaycap.ResetText();
            txt_Noicap.ResetText();
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            cbo_Manhanvien.DataBindings.Clear();
            cbo_Machinhtri.DataBindings.Clear();
            cbo_Chungnhan.DataBindings.Clear();
            dte_Ngaycap.DataBindings.Clear();
            txt_Noicap.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            cbo_Machinhtri.DataBindings.Add("EditValue", _dtb, ".MA_TDCT");
            cbo_Manhanvien.DataBindings.Add("EditValue", _dtb, ".MANHANVIEN");
            cbo_Chungnhan.DataBindings.Add("EditValue", _dtb, ".CHUNGNHAN");
            dte_Ngaycap.DataBindings.Add("EditValue", _dtb, ".NGAYCAP_CNCT");
            txt_Noicap.DataBindings.Add("Text", _dtb, ".NOICAP_CNCT");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            cbo_Manhanvien.Properties.ReadOnly = isReadOnly;
            cbo_Machinhtri.Properties.ReadOnly = isReadOnly;
            cbo_Chungnhan.Properties.ReadOnly = isReadOnly;
            dte_Ngaycap.Properties.ReadOnly = isReadOnly;
            txt_Noicap.Properties.ReadOnly = isReadOnly;

            grcCochinhtri.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            bto_Search.Visible = true;
            grcCochinhtri.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bto_Search_Click(object sender, EventArgs e)
        {

            try
            {
                var o = new DAL.Entities.Cochinhtri()
                {
                    MA_TDCT = Convert.ToString(cbo_Machinhtri.EditValue),
                    MANHANVIEN = Convert.ToString(cbo_Manhanvien.EditValue),
                    CHUNGNHAN=Convert.ToString(cbo_Chungnhan.EditValue),
                    NGAYCAP_CNCT=Convert.ToDateTime(dte_Ngaycap.EditValue),
                    NOICAP_CNCT = Convert.ToString(txt_Noicap.Text)
                };

                grcCochinhtri.DataSource = _dal.Search(o);
            }
            catch (Exception)
            {

                XtraMessageBox.Show("Lỗi! Không tìm thấy phiếu mượn này", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();
            _dtb1 = _dal.Select_Chinhtri();
            _dtb2 = _dal1.Select();
            _dtb3 = _dal.Select_Chungnhan();
            cbo_Machinhtri.Properties.DataSource = _dtb1;
            cbo_Manhanvien.Properties.DataSource = _dtb2;
            cbo_Chungnhan.Properties.DataSource = _dtb3;

            try
            {
                if (_dtb != null)
                {
                    grcCochinhtri.DataSource = _dtb;
                    AppGridView.SummaryField(grvCochinhtri, "MANHANVIEN", "Tổng = {0:#,0} ", DevExpress.Data.SummaryItemType.Count);
                    grvCochinhtri.OptionsBehavior.ReadOnly = true;
                    grvCochinhtri.OptionsView.ColumnAutoWidth = true;

                }

                base.LoadData();
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        #endregion

        #region Form Load
        private void grcCochinhtri_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion
    }
}
